<?php
/**
 * $Author: BEESSHOP $
 * ============================================================================
 * ============================================================================
 */
namespace app\module\controllers;

use app\models\AdRecorder;
use app\models\AmountRecorder;
use app\models\CateRecorder;
use app\models\CouponRecorder;
use app\models\DiscountRecorder;
use app\models\GoodsRecorder;
use app\models\MemberRecorder;
use app\models\NewRecorder;
use app\models\PurchaseRecorder;
use app\models\RateRecorder;
use app\models\RateuseRecorder;
use app\models\SetRecorder;

class IndexController extends BaseController{

    //优惠券调用
    public function actionYhq(){
        $list = CouponRecorder::find()->select(['count','reduce','p_type'])->orderBy('p_type DESC')->asArray()->all();
        $arr=[];
        foreach($list as $k=>$v){
            $name = ($v['p_type']==1)?"新人券":'';
            $arr[$k]['name'] = $name."满".$v['count']."减".$v['reduce'];
        }
        $result = ['result'=>1,'data'=>$arr];
        return json_encode($result);
    }

    //新人购调用
    public function actionNewusergoods(){
        $post = \Yii::$app->request->post('type');
        $num = intval($post['goods_num']);
        $num = empty($num)?5:$num;
        $list = GoodsRecorder::find()->select(['id','goods_name','thumb_pic','goods_num','goods_unit','goods_price','goods_hd_price',"goods_small_name"])->where("new_id!=0")->andWhere("is_down=0")->limit($num)->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['goods_small_name'] = empty($v['goods_small_name'])?'':$v['goods_small_name'];
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
        }
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result);
    }

    //分类调用
    public function actionNavlist(){
        $list = CateRecorder::find()->select(['id','cate_name','thumb_pic'])->where('is_show=1')->orderBy('cate_order DESC')->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
        }
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result);
    }

    //限时购调用
    public function actionXsgoods(){
        $list = GoodsRecorder::find()->select(['id','goods_name','thumb_pic','goods_num','goods_unit','goods_price','goods_hd_price',"goods_small_name","sell_num"])->where("purchase_id!=0")->andWhere("is_down=0")->limit(10)->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['goods_small_name'] = empty($v['goods_small_name'])?'':$v['goods_small_name'];
            $list[$k]['percent'] = $this->get_percent($v['goods_num'],$v['sell_num']);
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
        }
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result);
    }

    //商品分页列表调用，$page-当前页，$order_type-排序方式,$goods_type-模板标识
    public function actionGoods(){
        $post = \Yii::$app->request->post('page');
        $page = intval($post['page'])-1;
        $pagesize=\Yii::$app->params['pagesize'];
        $order_type = empty($post['order_type'])?'addtime DESC':$post['order_type'];
        $goods_type = intval($post['goods_type']);
        $offset=$page*$pagesize;
        $list = GoodsRecorder::find()->select(['id','goods_name','thumb_pic','goods_num','goods_unit','goods_price','goods_hd_price',"goods_small_name","sell_num",'new_id','discount_id','purchase_id','amount_id']);
        if($goods_type){
            $list = $list->andWhere('goods_type='.$goods_type);
        }
        $list=$list->andWhere("is_down=0")
            ->offset($offset)->limit($pagesize)->orderBy($order_type)->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['goods_small_name'] = empty($v['goods_small_name'])?'':$v['goods_small_name'];
            $list[$k]['percent'] = $this->get_percent($v['goods_num'],$v['sell_num']);
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
            if($v['discount_id']){
                $model = DiscountRecorder::find()->where('id='.$v['discount_id'])->andWhere('status=0')->one();
                if($model){
                    $d = json_decode($model->content,true);
                    $list[$k]['discount_id'] = "满".$d[0]['count']."件打".$d[0]['reduce']."折";
                }
            }
            if($v['new_id']){
                $model = NewRecorder::find()->where('id='.$v['new_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['new_id'] = $model->title;
                }
            }
            if($v['purchase_id']){
                $model = PurchaseRecorder::find()->where('id='.$v['purchase_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['purchase_id'] = $model->title;
                }
            }
            if($v['amount_id']){
                $model = AmountRecorder::find()->where('id='.$v['amount_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['amount_id'] = $model->title;
                }
            }
        }
        if(count($list)){
            $result = ['result'=>1,'data'=>$list];
        }else{
            $result = ['result'=>0,'data'=>'没有更多内容'];
        }

        return json_encode($result);
    }

    //轮播图列表
    public function actionAdlist(){
        $list = AdRecorder::find()->select(['id','thumb_pic'])->orderBy('ad_order DESC')->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
        }
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result);
    }

    //获取网站名称
    public function actionInfo(){
        $info = SetRecorder::find()->where('set_type=1')->asArray()->one();
        $content = json_decode($info['content'],true);
        $data['web_name'] = $content['web_name'];
        $data['key'] = empty($content['key'])?[]:explode("|",$content['key']);
        $result = ['result'=>1,'data'=>$data];
        return json_encode($result);
    }

    //获取模板标识商品
    //$type-模板标识,$num-读取数量
    //参数格式{type:{goods_type:1,num:1,cate:0,order:'addtime DESC',rand:0}}
    public function actionGoodsmb(){
        $post = \Yii::$app->request->post('type');
        $type=intval($post['goods_type']);
        $num = intval($post['num']);
        $num = empty($num)?5:abs($num);
        $cate = intval($post['cate']);
        $order = $post['order'];
        $rand = intval($post['rand']);//随机
        $list = GoodsRecorder::find()->select(['id','pics','goods_name','thumb_pic','goods_num','goods_unit','goods_price','goods_hd_price',"goods_small_name","sell_num",'new_id','discount_id','purchase_id','amount_id']);
        if($type){
            $list = $list->andWhere('goods_type=:type',[':type' => $type]);
        }
        if($cate){
            $list = $list->andWhere('cate_id=:cate',[':cate' => $cate]);
        }
        if($order){
            $list = $list->orderBy($order);
        }
        if($rand){
            $list = $list->orderBy('RAND()');
        }
        $list=$list->limit($num)->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
            $v['pics'] = json_decode($v['pics'],true);
            foreach($v['pics'] as $pk=>$pv){
                $v['pics'][$pk] = \Yii::$app->params['base_url'].$pv;
            }
            $list[$k]['pics'] = $v['pics'];
            if($v['discount_id']){
                $model = DiscountRecorder::find()->where('id='.$v['discount_id'])->andWhere('status=0')->one();
                if($model){
                    $d = json_decode($model->content,true);
                    $list[$k]['discount_id'] = "满".$d[0]['count']."件打".$d[0]['reduce']."折";
                }
            }
            if($v['new_id']){
                $model = NewRecorder::find()->where('id='.$v['new_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['new_id'] = $model->title;
                }
            }
            if($v['purchase_id']){
                $model = PurchaseRecorder::find()->where('id='.$v['purchase_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['purchase_id'] = $model->title;
                }
            }
            if($v['amount_id']){
                $model = AmountRecorder::find()->where('id='.$v['amount_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['amount_id'] = $model->title;
                }
            }

        }
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result);
    }

    //获取顶级分类
    public function actionParentcate(){
        $list = CateRecorder::find()->select(['id','cate_name','thumb_pic'])->where('parent_id=0')->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['thumb_pic'] = $v['thumb_pic'];
        }
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result);
    }

    //获取随机商品
    //参数：$order-排序，如："time DESC",$num-数量
    public function actionRandgoods(){
        $post = \Yii::$app->request->post('type');
        $order = $post['order'];
        $order = empty($order)?"addtime DESC":$order;
        $num = intval($post['num']);
        $num = empty($num)?10:$num;
        $list = GoodsRecorder::find()->select(['id','pics','goods_name','thumb_pic','goods_num','goods_unit','goods_price','goods_hd_price',"goods_small_name","sell_num",'new_id','discount_id','purchase_id','amount_id']);
        $list = $list->andWhere('is_down=0')->orderBy('RAND()')->orderBy($order)->limit($num)->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
            $v['pics'] = json_decode($v['pics'],true);
            foreach($v['pics'] as $pk=>$pv){
                $v['pics'][$pk] = \Yii::$app->params['base_url'].$pv;
            }
            $list[$k]['pics'] = $v['pics'];
            if($v['discount_id']){
                $model = DiscountRecorder::find()->where('id='.$v['discount_id'])->andWhere('status=0')->one();
                if($model){
                    $d = json_decode($model->content,true);
                    $list[$k]['discount_id'] = "满".$d[0]['count']."件打".$d[0]['reduce']."折";
                }
            }
            if($v['new_id']){
                $model = NewRecorder::find()->where('id='.$v['new_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['new_id'] = $model->title;
                }
            }
            if($v['purchase_id']){
                $model = PurchaseRecorder::find()->where('id='.$v['purchase_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['purchase_id'] = $model->title;
                }
            }
            if($v['amount_id']){
                $model = AmountRecorder::find()->where('id='.$v['amount_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['amount_id'] = $model->title;
                }
            }

        }
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result);
    }

    //关键词内容
    //参数：$key-关键词
    public function actionKeygoods(){
        $post = \Yii::$app->request->post('type');
        $key  = $post['key'];
        $list = GoodsRecorder::find()->select(['id','pics','goods_name','thumb_pic','goods_num','goods_unit','goods_price','goods_hd_price',"goods_small_name","sell_num",'new_id','discount_id','purchase_id','amount_id']);
        $list = $list->andWhere('is_down=0')->andWhere(['like','goods_name',$key])->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
            $v['pics'] = json_decode($v['pics'],true);
            foreach($v['pics'] as $pk=>$pv){
                $v['pics'][$pk] = \Yii::$app->params['base_url'].$pv;
            }
            $list[$k]['pics'] = $v['pics'];
            if($v['discount_id']){
                $model = DiscountRecorder::find()->where('id='.$v['discount_id'])->andWhere('status=0')->one();
                if($model){
                    $d = json_decode($model->content,true);
                    $list[$k]['discount_id'] = "满".$d[0]['count']."件打".$d[0]['reduce']."折";
                }
            }
            if($v['new_id']){
                $model = NewRecorder::find()->where('id='.$v['new_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['new_id'] = $model->title;
                }
            }
            if($v['purchase_id']){
                $model = PurchaseRecorder::find()->where('id='.$v['purchase_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['purchase_id'] = $model->title;
                }
            }
            if($v['amount_id']){
                $model = AmountRecorder::find()->where('id='.$v['amount_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['amount_id'] = $model->title;
                }
            }

        }
        if(count($list)>0){
            $result = ['result'=>1,'data'=>$list];
        }else{
            $result = ['result'=>0,'data'=>'没有相关内容'];
        }

        return json_encode($result);

    }

    //获取商品
    //参数：$order-排序,$num-数量
    public function actionNumgoods(){
        $post = \Yii::$app->request->post('type');
        $order = $post['order'];
        $num = intval($post['num']);
        $num = empty($num)?5:abs($num);
        $list = GoodsRecorder::find()->select(['id','pics','goods_name','thumb_pic','goods_num','goods_unit','goods_price','goods_hd_price',"goods_small_name","sell_num",'new_id','discount_id','purchase_id','amount_id']);
        $list= $list->orderBy($order)->limit($num)->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
            $v['pics'] = json_decode($v['pics'],true);
            foreach($v['pics'] as $pk=>$pv){
                $v['pics'][$pk] = \Yii::$app->params['base_url'].$pv;
            }
            $list[$k]['pics'] = $v['pics'];
            if($v['discount_id']){
                $model = DiscountRecorder::find()->where('id='.$v['discount_id'])->andWhere('status=0')->one();
                if($model){
                    $d = json_decode($model->content,true);
                    $list[$k]['discount_id'] = "满".$d[0]['count']."件打".$d[0]['reduce']."折";
                }
            }
            if($v['new_id']){
                $model = NewRecorder::find()->where('id='.$v['new_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['new_id'] = $model->title;
                }
            }
            if($v['purchase_id']){
                $model = PurchaseRecorder::find()->where('id='.$v['purchase_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['purchase_id'] = $model->title;
                }
            }
            if($v['amount_id']){
                $model = AmountRecorder::find()->where('id='.$v['amount_id'])->andWhere('status=0')->one();
                if($model){
                    $list[$k]['amount_id'] = $model->title;
                }
            }

        }
        if(count($list)>0){
            $result = ['result'=>1,'data'=>$list];
        }else{
            $result = ['result'=>0,'data'=>'没有相关内容'];
        }
        return json_encode($result);
    }
    
    
    
    //获取评价列表
    //参数：$page,格式：{page:{page:1,id:0}}
    public function actionRatelist(){
        $post = \Yii::$app->request->post('page');
        $page = intval($post['page'])-1;
        $id = intval($post['id']);
        $pagesize = \Yii::$app->params['pagesize'];
        $offset = $page*$pagesize;
        $list = RateRecorder::find()->select(['id','goods_id','user_id','pics','content','use_hits','addtime','rate','attr','hf_content','is_name'])->where('goods_id=:id',[':id' => $id]);
        $list = $list->limit($pagesize)->offset($offset)->orderBy('addtime DESC')->asArray()->all();
        foreach($list as $k=>$v){
            //获取会员
            $user = MemberRecorder::find()->where('id='.$v['user_id'])->one();
            if($user){
                $arr['user_pic'] = empty($user->user_pic)?\Yii::$app->params['non_pic']:$user->user_pic;
                $arr['user_name'] = (!empty($user->user_name))?$user->user_name:"微信用户";
            }else{
                $arr['user_pic'] = \Yii::$app->params['non_pic'];
                $arr['user_name'] = "微信用户";
            }
            if($v['is_name']){
                $arr['user_pic'] = \Yii::$app->params['non_pic'];
                $arr['user_name'] = "微信用户";
            }
            $arr['user_pic'] = \Yii::$app->params['base_url'].$arr['user_pic'];
            $list[$k]['user'] = $arr;
            $list[$k]['user_id']=0;
            $v['pics'] = json_decode($v['pics'],true);
            $pic_arr = [];
            foreach($v['pics'] as $pv){
                $pic_arr[] = str_replace("../",\Yii::$app->params['base_url'],$pv);
            }
            $list[$k]['pics'] = $pic_arr;
            $list[$k]['addtime'] = date('m-d');
            $list[$k]['attr'] = empty($v['attr'])?'':json_decode($v['attr'],true);
            $use_hits = RateuseRecorder::find()->where('rate_id='.$v['id'])->count();
            $use_hits = ($use_hits>=100)?"99+":$use_hits;
            $list[$k]['use_hits'] = $use_hits;
        }
        if(count($list)){
            $result = ['result'=>1,'data'=>$list];
        }else{
            $result = ['result'=>0,'data'=>'没有更多内容了'];
        }
        return json_encode($result);
    }

    public function actionUpnum(){
        $result = ['result'=>1,'data'=>\Yii::$app->params['rate_num']];
        return json_encode($result);
    }






}